package lesson19;

public class SimpleNotepade implements INotepade{
	private Page[] notepade;
	
	SimpleNotepade(int numberOfPages){		
		if(numberOfPages>0){
			notepade = new Page[numberOfPages];
		}else
			notepade = new Page[20];
		
		for (int i = 0; i < notepade.length; i++) {
			notepade[i] = new Page("Page "+(i+1));
		}
	}
	@Override
	public void addText(int pageNumber, String text) {
		if(validatePage(pageNumber)){
			Page page = notepade[pageNumber-1];
			page.addText(text);	
		}
			
	
	}
	private boolean validatePage(int pageNumber){
		return pageNumber<=notepade.length && pageNumber>0;
	}
	@Override
	public void replace(int pageNumber, String text) {
		if(validatePage(pageNumber)){
			Page page = notepade[pageNumber-1];
			page.deleteText();
			page.addText(text);
		}
	}

	@Override
	public void deleteText(Page selectedPage) {		
		for (int i = 0; i < notepade.length; i++) {
			if(selectedPage != null){
				if(notepade[i].equals(selectedPage)){
					selectedPage.deleteText();		
				}
			}
		}
		
		
	}

	@Override
	public void printPages() {
		for (int i = 0; i < notepade.length; i++) {
			System.out.println("printing...");
			if(notepade[i]!= null){
				System.out.println(notepade[i].getText());
			}
		}
		
	}
	public Page getPages(int numberOfPage){
		return notepade[numberOfPage-1];
	}
	
}
